/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onurkagan.ksnack_lib.KSnack;

import com.onurkagan.ksnack_lib.Animations.Fade;
import com.onurkagan.ksnack_lib.ResourceTable;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

/**
 * ksnack
 *
 * @author dev
 * @since 2021-07-01
 */
public class KSnack {
    /**
     * 视图
     */
    private Component snackView;
    /**
     * linf
     */
    private LayoutScatter linf;
    /**
     * 插入点
     */
    private ComponentContainer insertPoint;
    /**
     * 布局
     */
    public Component rlvHost;
    /**
     * btn
     */
    private Button btnAction;
    /**
     * 三种信息
     */
    private Text txtMessage;
    /**
     * 事件侦听器
     */
    private KSnackBarEventListener kSnackBarEventListener;
    /**
     * 显示动画
     */
    private AnimatorProperty inAnim;
    /**
     * 隐藏动画
     */
    private AnimatorProperty outAnim;

    /**
     * KSnack
     *
     * @param component 组件
     */
    public KSnack(ComponentContainer component) {
        this.initializeKSnackBar(component);
    }

    /**
     * 初始化ksnack
     *
     * @param component 组件
     */
    private void initializeKSnackBar(ComponentContainer component) {
        insertPoint = (ComponentContainer) component;
        linf = LayoutScatter.getInstance(component.getContext());

        // Create view.
        snackView = linf.parse(ResourceTable.Layout_layout_snack_normal, insertPoint, false);
        snackView.setVisibility(component.INVISIBLE);

        // Initialize component view.
        rlvHost = (DependentLayout) snackView.findComponentById(ResourceTable.Id_normal_snack_bar_rlv);
        component.addComponent(snackView);

        // Initialize textview.
        txtMessage = (Text) rlvHost.findComponentById(ResourceTable.Id_snack_bar_txt_message);

        // Action button.
        btnAction = (Button) rlvHost.findComponentById(ResourceTable.Id_snack_bar_btn_action);

        // Set default in anim.
        inAnim = Fade.In.getAnimation(snackView);

        // Set default out anim.
        outAnim = Fade.Out.getAnimation(snackView);
    }

    /**
     * 得到视图
     *
     * @return {@link Component}
     */// Get view.
    public Component getSnackView() {
        return rlvHost;
    }

    // Message.

    /**
     * 设置消息
     * setMessage
     *
     * @param message 消息
     * @return this
     */
    public KSnack setMessage(String message) {
        // Check null message.
        if (message == null) {
            message = "n/a";
        }
        txtMessage.setText(message);
        return this;
    }

    // Set button action.

    /**
     * 设置操作
     * setAction
     *
     * @param buttonText 按钮文字
     * @param clickListener 点击监听器
     * @return this
     */
    public KSnack setAction(String buttonText, final Component.ClickedListener clickListener) {
        // Change button visibility.
        btnAction.setVisibility(Component.VISIBLE);

        // Set button text.
        btnAction.setText(buttonText);

        // Set onClickListener.
        btnAction.setClickedListener(clickListener);
        return this;
    }

    // Set Listener.

    /**
     * 设置监听器
     * setListener
     *
     * @param listener 侦听器
     * @return this
     */
    public KSnack setListener(KSnackBarEventListener listener) {
        this.kSnackBarEventListener = listener;
        return this;
    }

    // Set animation.

    /**
     * 设置动画
     * setAnimation
     *
     * @param inAnim 在动物
     * @param outAnim 从动物
     * @return this
     */
    public KSnack setAnimation(AnimatorProperty inAnim, AnimatorProperty outAnim) {
        this.inAnim = inAnim;
        this.outAnim = outAnim;
        return this;
    }

    /**
     * 显示
     * show
     */
    public void show() {
        // Animation listener.
        inAnim.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                snackView.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                snackView.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });

        // Set animation to view.
        inAnim.start();

        // Start callback.
        if (kSnackBarEventListener != null) {
            kSnackBarEventListener.showedSnackBar();
        }
    }

    /**
     * 驳回
     * dismiss
     */
    public void dismiss() {
        // Animation listener.
        outAnim.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                snackView.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                snackView.setVisibility(Component.INVISIBLE);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });

        // Set animation to view.
        outAnim.start();

        // Stop callback.
        if (kSnackBarEventListener != null) {
            kSnackBarEventListener.stoppedSnackBar();
        }
    }
}
